<template>
  <div class="login-container">
    <div class="login-box">
      <div class="login-left">
        <div class="image-wrapper">
          <img src="../assets/login/cart.jpg" alt="购物车图标" />
        </div>
      </div>
      <div class="login-form">
        <div class="form-header">
          <h2>校园二手交易平台</h2>
          <p class="subtitle">欢迎回来，请登录您的账号</p>
        </div>
        <div class="form-item">
          <label>账号</label>
          <input type="text" v-model="username" placeholder="请输入您的账号" />
        </div>
        <div class="form-item">
          <label>密码</label>
          <input type="password" v-model="password" placeholder="请输入您的密码" />
        </div>
        <button class="login-btn" @click="handleLogin">登 录</button>
        <div class="form-footer">
          <span class="forgot-pwd">忘记密码？</span>
          <router-link to="/register" class="register-link">立即注册</router-link>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from "vue";
import { LoginApi } from '../api/login';
import { useRouter } from "vue-router";
import { ElMessage } from 'element-plus'
const username = ref("");
const password = ref("");
const router = useRouter()
const handleRegister = async () => {
  router.push("/register")
}
const handleLogin = async () => {
    try {
        const response = await LoginApi.login({
            mobile: username.value.trim(),
            password: password.value.trim()

        });
        const { data } = response;
        localStorage.setItem("accessToken",response.data.data.accessToken)
        localStorage.setItem("userId",response.data.data.userId)
        ElMessage.success('登录成功');
        router.push("/center")
       
        // 这里可以添加登录成功后的逻辑，比如存储 token、跳转到首页等
    } catch (error) {
      ElMessage.error('登录失败，请检查账号密码'); 
        // 这里可以添加登录失败后的提示逻辑
    }
};


const getFavorites = async () => {
    try {
        const response = await LoginApi.getFavorites({

        });
        console.log('成功', response);
     
    } catch (error) {
        console.error('失败:', error);

    }
};
</script>

<style scoped>
.login-container {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #f5f7fa 0%, #e4e9f2 100%);
  padding: 20px;
}

.login-box {
  display: flex;
  background: white;
  border-radius: 20px;
  box-shadow: 0 15px 35px rgba(0, 0, 0, 0.1);
  overflow: hidden;
  width: 900px;
  max-width: 95%;
  position: relative;
}

.login-left {
  flex: 1;
  background: linear-gradient(135deg, #4CAF50 0%, #2E7D32 100%);
  padding: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.image-wrapper {
  width: 280px;
  height: 280px;
  border-radius: 20px;
  overflow: hidden;
  box-shadow: 0 10px 20px rgba(0, 0, 0, 0.2);
}

.login-left img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.3s ease;
}

.login-left img:hover {
  transform: scale(1.05);
}

.login-form {
  flex: 1;
  padding: 40px;
  display: flex;
  flex-direction: column;
}

.form-header {
  margin-bottom: 40px;
  text-align: center;
}

h2 {
  color: #2E7D32;
  font-size: 28px;
  font-weight: 600;
  margin-bottom: 10px;
}

.subtitle {
  color: #666;
  font-size: 16px;
}

.form-item {
  margin-bottom: 25px;
}

label {
  display: block;
  margin-bottom: 8px;
  color: #333;
  font-weight: 500;
}

input {
  width: 100%;
  padding: 12px 16px;
  border: 2px solid #e0e0e0;
  border-radius: 12px;
  font-size: 15px;
  transition: all 0.3s ease;
  background: #f8f9fa;
}

input:focus {
  border-color: #4CAF50;
  background: white;
  box-shadow: 0 0 0 4px rgba(76, 175, 80, 0.1);
}

.login-btn {
  width: 100%;
  padding: 14px;
  background: #4CAF50;
  color: white;
  border: none;
  border-radius: 12px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
  margin-top: 10px;
}

.login-btn:hover {
  background: #2E7D32;
  transform: translateY(-2px);
  box-shadow: 0 6px 15px rgba(46, 125, 50, 0.3);
}

.login-btn:active {
  transform: translateY(0);
}

.form-footer {
  margin-top: 25px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 14px;
}

.forgot-pwd,
.register-link {
  color: #666;
  text-decoration: none;
  transition: color 0.3s ease;
  font-weight: 500;
}

.forgot-pwd:hover,
.register-link:hover {
  color: #2E7D32;
}

@media (max-width: 768px) {
  .login-box {
    flex-direction: column;
  }

  .login-left {
    padding: 30px;
  }

  .image-wrapper {
    width: 200px;
    height: 200px;
  }

  .login-form {
    padding: 30px;
  }
}
</style>
